layui.use([ 'form', 'laydate', 'util' ],function() {
	var $ = layui.jquery, 
			form = layui.form, 
			laydate = layui.laydate;

	var province = $("#province"), 
		city = $("#city"), 
		district = $("#district");

	// 为省下拉框赋值
	$.ajax({
		type : "get",
		url : 'system/findAllProvince',
		success : function(result) {
			// province = result;
			var province = "<option value=''></option>";
			for (var i = 0; i < result.length; i++) {
				province += "<option value='"
						+ result[i].province + "'>"
						+ result[i].province + "</option>";
			}
			$("#province").append(province);
			$("#provinces").append(province);
			// console.info(mh_options)
			form.render('select');
		}
	});

	// 为市下拉框赋值
	form.on('select(province)', function(data) {
		$("#city").html("");
		$("#district").html('');
		var province = data.value;
		$.ajax({
			type : "get",
			data : {
				province : province
			},
			url : 'system/findAllCity',
			success : function(result) {
				var city = "<option value=''>请选择市</option>";
				for (var i = 0; i < result.length; i++) {
					city += "<option value='" + result[i].city
							+ "'>" + result[i].city
							+ "</option>";
				}
				$("#city").append(city);
				// console.info(mh_options)
				form.render('select');
			}
		});
	});

	// 为县(区)下拉框赋值
	form.on('select(city)',function(data) {
		$("#district").html('');
		var city = data.value;
		$.ajax({
			type : "get",
			data : {
				city : city
			},
			url : 'system/findAllDistrict',
			success : function(result) {
				var district = "<option value=''>请选择县(区)</option>";
				for (var i = 0; i < result.length; i++) {
					district += "<option value='"
							+ result[i].district
							+ "'>"
							+ result[i].district
							+ "</option>";
					}
				$("#district").append(district);
				// console.info(mh_options)
				form.render('select');
				}
			});
		});
	// -------------------------------------------------------------------------------------------------
	// 为市下拉框赋值
	form.on('select(provinces)', function(data) {
		$("#citys").html("");
		$("#districts").html('');
		var province = data.value;
		$.ajax({
			type : "get",
			data : {
				province : province
			},
			url : 'system/findAllCity',
			success : function(result) {
				var citys = "<option value=''>请选择市</option>";
				for (var i = 0; i < result.length; i++) {
					citys += "<option value='" + result[i].city
							+ "'>" + result[i].city
							+ "</option>";
				}
				$("#citys").append(citys);
				// console.info(mh_options)
				form.render('select');
				}
			});
		});

	// 为县(区)下拉框赋值
	form.on('select(citys)',function(data) {
		$("#districts").html('');
		var city = data.value;
		$.ajax({type : "get",
			data : {
				city : city
			},
			url : 'system/findAllDistrict',
			success : function(result) {
				var districts = "<option value=''>请选择县(区)</option>";
				for (var i = 0; i < result.length; i++) {
					districts += "<option value='"
							+ result[i].district
							+ "'>"
							+ result[i].district
							+ "</option>";
				}
				$("#districts").append(districts);
				//console.info(districts)
				form.render('select');
				}
			});
		});

	//时间
	laydate.render({
		elem : '#arrivalTime',
		format : 'yyyy-MM-dd'
	});

	form.on('submit(calFormBtn)', function(data){
		  
		  //console.log(data.elem) //被执行事件的元素DOM对象，一般为button对象
		  //console.log(data.form) //被执行提交的form对象，一般在存在form标签时才会返回
		  //console.log(data.field.province) //当前容器的全部表单字段，名值对形式：{name: value}
		  //要替换的字符串数组
		  var bthdzfc = ["省", "市", "自治区", "回族", "壮族", "维吾尔"];
		  var startProvince = data.field.province;
		  var endProvince = data.field.provinces;
		  //替换掉数据库中不包含的字符串 不然查询不到数据
		  for (var i in bthdzfc) {
			  startProvince = startProvince.replace(bthdzfc[i], "");
			  endProvince = endProvince.replace(bthdzfc[i], "");
		  }
		  $.ajax({
			  type:'get',
			  data:{
				  startProvince: startProvince,
				  startCity: data.field.city,
				  startCounty: data.field.district,
				  endProvince: endProvince,
				  endCity: data.field.citys,
				  endCounty: data.field.districts,
				  weight: data.field.headWeight,
				  postTime:new Date(data.field.arrivalTime)
				  
			  },
			  url:'system/calFee',
			  success: function(res){
				  $("#yf").text(res.fee);
				  var date = layui.util.toDateString(res.arravDate, 'yyyy年MM月dd日');
				  $("#sj").text(date);
			  }
		  });
		  return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
		});
})